<template>
	<view style="height: 100%">
		<top title="首页" color="#fff" bgc="transparent" :show="2" position="absolute"></top>
		<image src="https://cdn.xtwaixie.com/uploads/20240705/17201808706687e08666656.png" class="home-bg"></image>
		<view class="pr">
			<view class="add-box" v-if="add_show == 1">
				<view class="add-sjx"></view>
				<view class="add-cont df ac sb">
					<image class="img12" src="https://wx.xtwaixie.com/img/close3.png" @tap="onAddClose"></image>
					<p class="f14 cf ml12">添加到我的小程序 打开更方便</p>
					<!-- <button class="add-but">添加</button> -->
				</view>
			</view>
			<!-- 轮播 -->
			<swiper class="home-swiper" :indicator-dots="true" :circular="true" indicator-color="rgba(255,255,255,.3)"
				indicator-active-color="#fff" :autoplay="true" interval="5000">
				<swiper-item v-for="(item, index) in home_list" :key="index">
					<image mode="aspectFill" class="swiper-img" referrerPolicy='no-referrer' :src="item.thumb"
						@tap="onSwiper" :data-item="item" />
				</swiper-item>
			</swiper>
			<!-- 跳转 -->
			<ul class="db">
				<li class="df ac sb mt20">
					<navigator class="w166 pd15 bgcf br8 brs df ac sb" url="/pages/demand/subDemand" hover-class="none"
						open-type="navigate">
						<view class="f1">
							<h2 class="db f600 f16 c28">免费询价</h2>
							<p class="db f12 c9d mt2">最快一分钟报价</p>
						</view>
						<image class="img32" src="https://wx.xtwaixie.com/img/c3.png" />
					</navigator>
					<navigator class="w166 pd15 bgcf br8 brs df ac sb" url="/pages/demand/demand" hover-class="none"
						open-type="navigate">
						<view class="f1">
							<h2 class="db f600 f16 c28">订单大厅</h2>
							<p class="db f12 c9d mt2">最新订单列表</p>
						</view>
						<image class="img32" src="https://wx.xtwaixie.com/img/c4.png" />
					</navigator>
				</li>
				<li class="df ac sb mt10">
					<navigator class="w166 pd15 bgcf br8 brs df ac sb" url="/pages/myInquiry/myInquiry"
						hover-class="none" open-type="navigate">
						<view class="f1">
							<h2 class="db f600 f16 c28">采购管理</h2>
							<p class="db f12 c9d mt2">查看工厂报价</p>
						</view>
						<image class="img32" src="https://wx.xtwaixie.com/img/c1.png" />
					</navigator>
					<!--                   <navigator class="w166 pd15 bgcf br8 brs df ac sb" url="/pages/shopping/shopping" hover-class="none" open-type="navigate">
                        <view class="f1">
                            <h2 class="db f600 f16 c28">刀具商城</h2>
                            <p class="db f12 c9d mt2">买刀具送积分</p>
                        </view>
                        <image class="img32" src="https://wx.xtwaixie.com/img/c1.png" />
                    </navigator> -->
					<navigator class="w166 pd15 bgcf br8 brs df ac sb" url="/pages/settled/setPage" hover-class="none"
						open-type="navigate" v-if="grade == 0">
						<view class="f1">
							<h2 class="db f600 f16 c28">入驻供应商</h2>
							<p class="db f12 c9d mt2 " style="color: #f53d10;">免费接单</p>
						</view>
						<image class="img32" src="https://wx.xtwaixie.com/img/c2.png" />
					</navigator>
					<navigator class="w166 pd15 bgcf br8 brs df ac sb" url="/pages/myQuotation/myQuotation"
						hover-class="none" open-type="navigate" v-else>
						<view class="f1">
							<h2 class="db f600 f16 c28">我的报价</h2>
							<p class="db f12 c9d mt2">查看报价记录</p>
						</view>
						<image class="img32" src="https://wx.xtwaixie.com/img/c2.png" />
					</navigator>
				</li>
			</ul>
			<view class="" style="margin: 25rpx 0;border-radius:16rpx;overflow: hidden;" v-if='content'>
				<van-notice-bar left-icon="volume-o" :text="content" color='#283043' background='#FFFFFF' />
			</view>
			<view class="" style="height: 50rpx;" v-else>

			</view>
			<!-- 最新需求 -->
			<view class="" v-if="info.demand.length != 0">
				<view class="df ac sb">
					<view class="">
						<h2 class="f18 c28 f600">最新订单</h2>
					</view>
					<view class="moreList" @tap="go">
						更多订单
					</view>
					<!-- <navigator class="df ac" url="/pages/demand/demand" hover-class="none" open-type="navigate">
        <p class='f12 c9d mr5'>查看更多</p>
        <image class="more" src="../../img/more.png" />
			</navigator> -->

					<!--                   <view class="bgc10 br12 df ac sb pd510 fl ml10">
                        <p class="f12 cf">关注公众号，免费获取最新提醒</p>
                        <view class="gz-but1 ml10" @tap="goWeb">关注</view>
                    </view> -->
				</view>
				<ul class="mt15 db">
					<block v-for="(item, index) in info.demand" :key="index">
						<!-- <navigator class="df ac sb pd15 bgcf br8 mt10" url="/pages/demand/demandDetails?id={{item.id}}" hover-class="none" open-type="navigate">
          <view class='f1'>
            <view class=''>
              <p class='tag'>{{item.machining_name}}</p>
              <h2 class='f14 f600 c28 ml8'>{{item.spare_parts_name}}</h2>
            </view>
            <view class='df ac mt5'>
              <image class="img12 mr5" src="../../img/dq.png" />
              <P class='f12 c6b'>{{item.province}} {{item.city}}</P>
              <view class='line'></view>
              <p class='f12 c10'>共{{item.number}}件</p>
              <p class='tag tag1 ml4'>{{item.view_integral}}积分</p>
            </view>
            <view class='df ac mt5'>
              <p class='f12 c9d'>{{item.company}}</p>
              <p class='f12 c9d ml10'>报价截止：{{item.end_at}}</p>
            </view>
          </view>
          <image class="img72 ml15" src="{{item.redrawing}}" />
        </navigator> -->
						<view @tap="demandDetails(item.id)" class="df ac sb pd15 bgcf br8 mt10"
							style="align-items: center;"
							:style="{padding:item.is_quotation !== 0 ? '11.33rpx 30rpx' : '30rpx'}">
							<view class="f1 mr10">
								<view class="">
									<!-- <p class='tag mr8' wx:if='{{item.machining_name}}'>{{item.machining_name}}</p> -->
									<P :class="'f14  f600 ' + (item.is_end == 1 || item.is_deal == 1 ? 'c3c' : 'c28')">
										{{ item.spare_parts_name }}
									</P>
								</view>
								<view class="df ac mt8">
									<image class="img12" src="https://wx.xtwaixie.com/img/dz2.png" />
									<span
										:class="'f12 c6b ml4 ' + (item.is_end == 1 || item.is_deal == 1 ? 'c3c' : 'c28')">{{ item.province }}
										{{ item.city }}</span>
									<p class="line"></p>
									<!-- <span class='c10 f500 f12 {{item.is_end==1?"c3c":"c28"}}'>共{{item.count}}件</span> -->
									<span :class="'c10 f500 f12 ' + (item.is_end == 1 || item.is_deal == 1 ? 'c3c' : 'c28')">
										{{ item.parts_count >= 2 ? '共' + item.parts_count + '款' : item.parts_piece ? '共' + item.count + item.parts_piece : '' }}
									</span>
									<p class="tag tag1 ml4" v-if="grade > 0 && item.view_type > 0">
										{{ item.view_type === 1 ? '免费' : 'VIP' }}
									</p>
									<!-- <p class="tag tag1 ml4">{{ item.view_integral }}积分</p> -->
								</view>
								<view class="df ac mt10">
									<p :class="'f12 c9d ' + (item.is_end == 1 || item.is_deal == 1 ? 'c3c' : 'c28')">{{ item.company }}</p>
									<p :class="'f12 c9d ml10 ' + (item.is_end == 1 || item.is_deal == 1 ? 'c3c' : 'c28')">
										报价截止：{{ item.end_at }}</p>
								</view>
							</view>
							<view class="img72 pr" hover-class="none" :hover-stop-propagation="false"
								style="height: auto;">
								<view class="quotationNum" v-if="item.is_quotation !== 0" :class="{gray:item.is_end == 1 || item.is_deal == 1}">
									已报价:{{item.quotation_count}}
								</view>
								<image class="img72" referrerPolicy='no-referrer'
									:src="getImgSrc(item.redrawing) ? getImgSrc(item.redrawing) : ''"
									 style="vertical-align: bottom;" :class="{gray:item.is_end == 1 || item.is_deal == 1}"/>
								<p class="bj" v-if="item.is_deal == 1" :class="{gray:item.is_end == 1 || item.is_deal == 1}">已完成</p>
								<p class="bj" v-else-if="item.is_end == 2 && item.is_deal == 0">报价中</p>
								<p class="bj" v-else :class="{gray:item.is_end == 1 || item.is_deal == 1}">已完成</p>
								<image class="yjs" mode="widthFix" src="https://cdn.xtwaixie.com/uploads/20240415/1713167495661cdc8768cb9.png"
									style="left: -100%;width: 140rpx;top: 50%;transform: translateY(-50%);filter: none !important;"
									v-if="item.is_deal == 1 || item.is_end == 1"></image>
							</view>
						</view>
					</block>
				</ul>
			</view>
			<!-- 推荐商品 -->
			<view v-if="info.hotGoods.length != 0" :style="{'margin-top':info.demand.length != 0 ? '50rpx' : '0'}">
				<view class="df ac sb">
					<h2 class="db f18 f600 c28">推荐商品</h2>
					<view class="df ac search-home-box" @tap="goSearch">
						<image src="https://wx.xtwaixie.com/img/home-search.png" class="img12"></image>
						<P class="ml8 c28 f12">搜索商品名称</P>
					</view>

					<view class="df ac" @tap="goAbout">
						<image src="https://wx.xtwaixie.com/img/cz.png" class="img16"></image>
						<p class="f12 c6b ml4">如何搜索</p>
					</view>
				</view>
				<ul class="df ac fw mt15">
					<block v-for="(item, index) in info.hotGoods" :key="index">
						<navigator class="shop-list" :url="'/pages/order/shopDetail?id=' + item.id" hover-class="none"
							open-type="navigate">
							<image class="list-img" referrerPolicy='no-referrer' :src="item.thumb" />
							<view class="pd8">
								<p class="pd ovh2 f12 c28">{{ item.title }}</p>
								<view class="mt5 df ac sb">
									<h2 class="f14 db cff8">¥{{ item.price }}</h2>
									<!-- <p class="tag tag1 mt4">送积分</p> -->
								</view>
							</view>
						</navigator>
					</block>
				</ul>
			</view>
		</view>

		<button class="kfs-box" open-type="contact">
			<image class="kfs" src="https://wx.xtwaixie.com/img/kf3.png" @tap="onKfShow"></image>
		</button>
		<!-- <view class="kfs-box" >
	<image class="kfs" src="../../img/kf3.png" bindtap='onKfShow'></image>
	<view class="kfs-cont {{kf_show?'kf-out':'kf-in'}}">
		<view class="df ac h28 ml4">
			<button class="add-but" wx:if='{{kf_show}}' open-type="contact">咨询</button>
			<p class="f12 cf ml8">采购询价 图纸问题 接单服务</p>
		</view>
	</view>
</view> -->

		<tabbar></tabbar>
		<van-popup :show="showPopup" z-index='10000' position='bottom' :close-on-click-overlay='true'
			@close='showPopup = false' custom-style='background:rgba(0,0,0,0);' :safe-area-inset-bottom="false">
			<view class="popupBox" @click='showPopup = false'>
				<image referrerPolicy='no-referrer' :src="getImgSrc(publish_inquiry)" mode="widthFix" class="popupImg"
					@click.stop="goAdd()" style="width: 80%;"></image>
				<view style="height: 120rpx;width: 100%;">
					
				</view>
			</view>
		</van-popup>
		<van-popup :show="showTips" custom-style='background:#00000000;'>
			<image src="https://cdn.xtwaixie.com/uploads/20240108/1704702779659bb33b1136b.png" mode="widthFix" style="width: 450rpx;padding-bottom: 800rpx;"></image>
		</van-popup>
	</view>
</template>

<script>
	import tabbar from '@/component/tabbar/tabbar';
	import top from '@/component/top/top';
	import http from '../../utils/http';
	export default {
		components: {
			tabbar,
			top
		},
		data() {
			return {
				shuiyin: this.shuiyin,
				info: {
					demand: [],
					hotGoods: []
				},
				home_list: [],
				add_show: true,
				kf_show: false,
				grade: 0,
				content: '',
				showPopup: false,
				publish_inquiry: '',
				number_receive: false,
				remark: {},
				showTips: false
			};
		},
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad(options) {
			let add_show = uni.getStorageSync('add_show') || 1;
			console.log();
			this.setData({
				add_show: add_show
			});
		},
		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady() {},
		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow(options) {
			this.getBase();
			let currentZero = uni.getStorageSync('currentZero');
			if (currentZero) {
				let nowDate = new Date().setHours(0, 0, 0, 0)
				if (nowDate > currentZero) {
					uni.setStorageSync('currentZero', new Date().getTime())
					this.showPopup = true
				}
			} else {
				uni.setStorageSync('currentZero', new Date().getTime())
				this.showPopup = true
			}
			this.getHome();
			if (uni.getStorageSync('token')) {
				this.wxAuthLogin();
			}
			this.getContent()
		},
		/**
		 * 生命周期函数--监听页面隐藏
		 */
		onHide() {},
		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload() {},
		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh() {},
		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom() {},
		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage() {},
		methods: {
			demandDetails(id) {
				let first = uni.getStorageSync("firstPopup")
				if(!first){
					this.showTips = true
					uni.setStorageSync('firstPopup',true)
				}
				let popupTime = uni.getStorageSync('popupTime');
				if (popupTime) {
					let nowDate = new Date().setHours(0, 0, 0, 0)
					if (nowDate > popupTime) {
						let that = this
						uni.requestSubscribeMessage({
							tmplIds: ['Ps_TGNHTlkFPLHybskEltaWoAvSZ1GhsoXfiJBGwTjA'],
							success(res) {
								if (res.Ps_TGNHTlkFPLHybskEltaWoAvSZ1GhsoXfiJBGwTjA == 'accept') {
									http.request('demand/empower')
									uni.setStorageSync('popupTime', new Date().getTime())
								}
								that.showTips = false
								uni.navigateTo({
									url: '/pages/demand/demandDetails?id=' + id
								})
							}
						})
					}else{
						let that = this
						that.showTips = false
						uni.navigateTo({
							url: '/pages/demand/demandDetails?id=' + id
						})
					}
				} else {
					let that = this
					uni.requestSubscribeMessage({
						tmplIds: ['Ps_TGNHTlkFPLHybskEltaWoAvSZ1GhsoXfiJBGwTjA'],
						success(res) {
							if (res.Ps_TGNHTlkFPLHybskEltaWoAvSZ1GhsoXfiJBGwTjA == 'accept') {
								http.request('demand/empower')
								uni.setStorageSync('popupTime', new Date().getTime())
							}
							that.showTips = false
							uni.navigateTo({
								url: '/pages/demand/demandDetails?id=' + id
							})
						}
					})
				}
			},
			goAdd() {
				this.showPopup = false
				if (this.remark.publish_inquiry) {
					let that = this
					uni.switchTab({
						url: that.remark.publish_inquiry,
						fail: function(res) {
							uni.navigateTo({
								url: that.remark.publish_inquiry
							})
						}
					})
				} else {
					uni.navigateTo({
						url: '/pages/demand/subDemand',
						fail: function(res) {
							uni.switchTab({
								url: '/pages/demand/subDemand'
							})
						}
					})
				}
			},
			go() {
				uni.navigateTo({
					url: "/pages/demand/demand"
				})
			},
			getContent() {
				http.request('base/pageCount')
					.then((res) => {
						this.content = res.data.gg ? res.data.gg.replace(/<img/g, '<img class="rich-text-img"') : '';
						console.log(this.content);
					})
					.catch((res) => {
						uni.showToast({
							title: res.message,
							icon: 'none'
						});
					});
			},
			// 获取个人信息
			getUserInfo() {
				http.request('user/userInfo')
					.then((res) => {
						this.setData({
							grade: res.data.userInfo.grade
						});
						this.number_receive = res.data.userInfo.number_receive
					})
					.catch((res) => {
						uni.showToast({
							title: res.message,
							icon: 'none'
						});
					});
			},
			onKfShow() {
				this.setData({
					kf_show: !this.kf_show
				});
			},

			onAddClose() {
				this.setData({
					add_show: 2
				});
				uni.setStorageSync('add_show', 2);
			},

			onSwiper(e) {
				let item = e.currentTarget.dataset.item;
				if (item.url == '/pages/landing/landing') {
					uni.navigateTo({
						url: '/pages/landing/landing'
					});
					return
				}
				if (item.url_type === 6) {
					uni.navigateTo({
						url: '/pages/recharge/recharge'
					});
					// if (uni.getSystemInfoSync().platform === 'ios') {
					// 	uni.navigateTo({
					// 		url: '/pages/recharge/iosvip'
					// 	});
					// } else {
					// 	uni.navigateTo({
					// 		url: '/pages/recharge/recharge'
					// 	});
					// }
					return
				}
				switch (item.url_type) {
					case 1:
						uni.redirectTo({
							url: '/pages/demand/subDemand'
						});
						break;
					case 2:
						uni.navigateTo({
							url: '/pages/order/shopDetail?id=' + item.goods_id
						});
						break;
					case 3:
						uni.redirectTo({
							url: '/pages/shopping/shopping'
						});
						break;
					case 4:
						uni.navigateTo({
							url: '/pages/content/content?id=' + item.id
						});
						break;
					case 5:
						uni.navigateTo({
							url: '/pages/settled/setPage'
						});
						break;
					case 7:
						uni.navigateTo({
							url: '/subPack/out/out?url=' + item.url
						});
						break;
					case 8:
						uni.navigateTo({
							url: '/pages/demand/demand'
						});
						break;
					case 9:
						uni.navigateTo({
							url: '/pages/myQuotation/myQuotation'
						});
						break;
					case 10:
						uni.navigateTo({
							url: '/pages/myInquiry/myInquiry'
						});
						break;
				}
			},

			getHome() {
				http.request('base/indexData')
					.then((res) => {
						this.setData({
							info: res.data
						});
					})
					.catch((res) => {
						console.log(res);
					});
			},

			getBase() {
				http.request('base/configData')
					.then((res) => {
						this.publish_inquiry = res.data.publish_inquiry
						this.remark = res.data.remark
						this.setData({
							home_list: res.data.new_home_banner
						});
					})
					.catch((res) => {
						console.log(res);
					});
			},

			wxAuthLogin() {
				let that = this;
				uni.login({
					complete: (res) => {
						let data = {
							code: res.code
						};
						let h = {
							'content-type': 'application/x-www-form-urlencoded'
						};
						uni.request({
							url: http.appUrl + 'login/getLogin',
							data: data,
							method: 'POST',
							header: h,
							success: (res) => {
								uni.setStorageSync('token', res.data.data.token.token);
								uni.setStorageSync('is_login', res.data.code);
							},
							fail: (res) => {
								console.log(res);
							},
							complete: () => {
								that.getUserInfo()
							}
						});
					}
				});
			},

			// 去查看公众号
			goWeb() {
				uni.navigateTo({
					url: '/pages/webview/webview'
				});
			},

			goSearch() {
				uni.navigateTo({
					url: '/pages/shopping/search'
				});
			},

			goAbout() {
				uni.navigateTo({
					url: '/pages/about/about?type=10'
				});
			}
		}
	};
</script>
<style>
	@import './home.css';

	.popupBox {
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.popupImg {
		width: 550rpx;
	}

	.popupOut {
		width: 50rpx;
		height: 50rpx;
		margin-top: 100rpx;
	}
</style>